

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Example: gpu &mdash; Playdoh v0.3.1 documentation</title>
    <link rel="stylesheet" href="_static/default.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '',
        VERSION:     '0.3.1',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="_static/jquery.js"></script>
    <script type="text/javascript" src="_static/underscore.js"></script>
    <script type="text/javascript" src="_static/doctools.js"></script>
    <link rel="top" title="Playdoh v0.3.1 documentation" href="index.html" />
    <link rel="up" title="Examples" href="examples.html" />
    <link rel="next" title="Example: maximize" href="examples-maximize.html" />
    <link rel="prev" title="Example: map_shared" href="examples-map_shared.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="examples-maximize.html" title="Example: maximize"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="examples-map_shared.html" title="Example: map_shared"
             accesskey="P">previous</a> |</li>
        <li><a href="index.html">Playdoh v0.3.1 documentation</a> &raquo;</li>
          <li><a href="examples.html" accesskey="U">Examples</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="example-gpu">
<span id="id1"></span><h1>Example: gpu<a class="headerlink" href="#example-gpu" title="Permalink to this headline">¶</a></h1>
<p>Example of <a class="reference internal" href="reference.html#playdoh.map" title="playdoh.map"><tt class="xref py py-func docutils literal"><span class="pre">map()</span></tt></a> with a function loading CUDA code and running on GPUs.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">playdoh</span> <span class="kn">import</span> <span class="o">*</span>
<span class="kn">from</span> <span class="nn">numpy</span> <span class="kn">import</span> <span class="o">*</span>
<span class="kn">import</span> <span class="nn">pycuda</span>


<span class="c"># The function loading the CUDA code</span>
<span class="k">def</span> <span class="nf">fun</span><span class="p">(</span><span class="n">scale</span><span class="p">):</span>
    <span class="c"># The CUDA code, which multiplies a vector by a scale factor.</span>
    <span class="n">code</span> <span class="o">=</span> <span class="s">&#39;&#39;&#39;</span>
<span class="s">    __global__ void test(double *x, int n)</span>
<span class="s">    {</span>
<span class="s">     int i = blockIdx.x * blockDim.x + threadIdx.x;</span>
<span class="s">     if(i&gt;=n) return;</span>
<span class="s">     x[i] *= </span><span class="si">%d</span><span class="s">;</span>
<span class="s">    }</span>
<span class="s">    &#39;&#39;&#39;</span> <span class="o">%</span> <span class="n">scale</span>

    <span class="c"># Compile the CUDA code to GPU code</span>
    <span class="n">mod</span> <span class="o">=</span> <span class="n">pycuda</span><span class="o">.</span><span class="n">compiler</span><span class="o">.</span><span class="n">SourceModule</span><span class="p">(</span><span class="n">code</span><span class="p">)</span>

    <span class="c"># Transform the CUDA function into a Python function</span>
    <span class="n">f</span> <span class="o">=</span> <span class="n">mod</span><span class="o">.</span><span class="n">get_function</span><span class="p">(</span><span class="s">&#39;test&#39;</span><span class="p">)</span>

    <span class="c"># Create a vector on the GPU filled with 8 ones</span>
    <span class="n">x</span> <span class="o">=</span> <span class="n">pycuda</span><span class="o">.</span><span class="n">gpuarray</span><span class="o">.</span><span class="n">to_gpu</span><span class="p">(</span><span class="n">ones</span><span class="p">(</span><span class="mi">8</span><span class="p">))</span>

    <span class="c"># Start the function on the GPU</span>
    <span class="n">f</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">int32</span><span class="p">(</span><span class="mi">8</span><span class="p">),</span> <span class="n">block</span><span class="o">=</span><span class="p">(</span><span class="mi">8</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">))</span>

    <span class="c"># Load the result from the GPU to the CPU</span>
    <span class="n">y</span> <span class="o">=</span> <span class="n">x</span><span class="o">.</span><span class="n">get</span><span class="p">()</span>

    <span class="c"># Finally, return the result</span>
    <span class="k">return</span> <span class="n">y</span>

<span class="c"># This line is required on Windows, any call to a Playdoh function</span>
<span class="c"># must be done after this line on this OS.</span>
<span class="c"># See http://docs.python.org/library/multiprocessing.html#windows</span>
<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">&#39;__main__&#39;</span><span class="p">:</span>
    <span class="c"># Execute ``fun(2)`` and ``fun(3)`` on 1 GPU on this machine</span>
    <span class="c"># and return the result.</span>
    <span class="k">if</span> <span class="n">CANUSEGPU</span><span class="p">:</span>
        <span class="k">print</span> <span class="nb">map</span><span class="p">(</span><span class="n">fun</span><span class="p">,</span> <span class="p">[</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="n">gpu</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
</pre></div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="index.html">
              <img class="logo" src="_static/logo.png" alt="Logo"/>
            </a></p>
  <h4>Previous topic</h4>
  <p class="topless"><a href="examples-map_shared.html"
                        title="previous chapter">Example: map_shared</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="examples-maximize.html"
                        title="next chapter">Example: maximize</a></p>
  <h3>This Page</h3>
  <ul class="this-page-menu">
    <li><a href="_sources/examples-gpu.txt"
           rel="nofollow">Show Source</a></li>
  </ul>
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="search.html" method="get">
      <input type="text" name="q" size="18" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="examples-maximize.html" title="Example: maximize"
             >next</a> |</li>
        <li class="right" >
          <a href="examples-map_shared.html" title="Example: map_shared"
             >previous</a> |</li>
        <li><a href="index.html">Playdoh v0.3.1 documentation</a> &raquo;</li>
          <li><a href="examples.html" >Examples</a> &raquo;</li> 
      </ul>
    </div>

    <div class="footer">
        &copy; Copyright 2011, Cyrille Rossant, Bertrand Fontaine, Dan Goodman.
      Last updated on Aug 17, 2011.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.6.
    </div>
<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-6773961-3']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>

  </body>
</html>